home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-07-24 | 2.1 KB | 100 lines | [TEXT/MPS ] |
- /*
- VirtualWorld.cp
-
- Implementation of Virtual world maintenance class.
-
- This class maintains an A5 global space, as well as supporting
- initialization of virtual function tables at run-time.
-
- by Patrick Beard.
-
- based on TN 256.
-
- ©1990 by Patrick C. Beard. All rights reserved.
- */
-
- #ifndef __VIRTUALWORLD__
- #include "VirtualWorld.h"
- #endif
-
- #ifndef __QUICKDRAW__
- #include <QuickDraw.h>
- #endif
-
- extern "C" {
- void A5Init(Ptr); // function that sets up (initializes) the A5 world.
- long A5Size(void); // function that returns the size of our A5 world.
- void init_vtbls(void); // function that initializes virtual tables.
- }
-
- // utility routine to switch A5's. (lifted from OSUtils.h)
- pascal Ptr SwapA5(Ptr newA5)
- = {0x2F4D,0x0004,0x2A5F};
-
- // constructor: set up the current App's required A5 world.
-
- VirtualWorld::VirtualWorld(Boolean worldFloats)
- {
- MoveHigh();
- Lock();
-
- error = noErr; // default is no error.
- codeFloats = worldFloats;
- oldA5 = nil; // none established for them.
- worldSize = A5Size(); // store our size for speed.
- ourA5 = NewPtr(worldSize); // better not fail.
- if(!ourA5) {
- error = -1;
- return;
- }
-
- // initialize our globals.
- A5Init(ourA5 + worldSize - 32); // see TN 256.
-
- // call InitGraf to initialize quickdraw globals.
- GrafPtr aPort;
- GetPort(&aPort);
- Enter(); // go inside our world.
- InitGraf(&qd.thePort);
- SetPort(aPort);
-
- // if this code doesn't float, only call this function once.
- if(!codeFloats)
- init_vtbls();
-
- Leave(); // leave the virtual world.
-
- Unlock();
- }
-
- VirtualWorld::~VirtualWorld()
- {
- Leave(); // restore any saved A5.
- if(ourA5)
- DisposPtr(ourA5);
- }
-
- void VirtualWorld::Enter()
- {
- GrafPtr currPort;
- GetPort(&currPort);
-
- oldA5 = SwapA5(ourA5 + (worldSize - 32));
-
- SetPort(currPort);
-
- if(codeFloats) {
- // call virtual table initialization functions everytime our world is
- // entered. this way, if we've been relocated, the tables will be correct.
- init_vtbls();
- }
- }
-
- void VirtualWorld::Leave()
- {
- if(oldA5) {
- SwapA5(oldA5);
- oldA5 = nil; // clear it so we won't do it again.
- }
- }
-